package com.serwer.gui;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import java.awt.Font;
import java.awt.SystemColor;
import java.awt.Color;

import javax.swing.DefaultListModel;
import javax.swing.ImageIcon;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JTextArea;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeEvent;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class Uzytkownicy extends JFrame {

	private JPanel contentPane;
	private JTable table;
	private JTextField textField;
	private JTextField textField_1;
	private JTextField textField_2;
	private JTable table_1;
	private JTextField textField_3;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					String admin = null;
					Uzytkownicy frame = new Uzytkownicy(admin);
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public Uzytkownicy(String admin) {
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 450, 313);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JLabel lblNewLabel = new JLabel("Zalogowany jako:");
		lblNewLabel.setFont(new Font("Tahoma", Font.PLAIN, 11));
		lblNewLabel.setBounds(73, 254, 85, 14);
		contentPane.add(lblNewLabel);
		
		JLabel lblNewLabel_1 = new JLabel("New label");
		lblNewLabel_1.setBounds(161, 254, 85, 14);
		contentPane.add(lblNewLabel_1);
		lblNewLabel_1.setText(admin);
		
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(10, 44, 124, 137);
		contentPane.add(scrollPane);
		
		table = new JTable();
		table.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent arg0) {
				textField.setText("");
				textField_1.setText("");
				textField_2.setText("");
				textField_3.setText("");
				int row = table.getSelectedRow();
	            String Table_click = (table.getModel().getValueAt(row, 1).toString());
	            String Table_click_id = (table.getModel().getValueAt(row, 0).toString());
	            System.out.println(Table_click);
	            String sql="select u.username, u.status, u.active, ind.index_name from indexy as ind left join uprawnienia as upr using(id_index) join user as u where upr.id_user =(select id_user from user where username='"+Table_click+"') AND u.id_user =(select id_user from uprawnienia where id_user='"+Table_click_id+"')";
	          
	           String sqlwww="Select username, status, active from user where username='"+Table_click+"'";
	           System.out.println(sql);
	            String sql2s="select u.username, u.status, u.active, upr.id_index from user as u join uprawnienia as upr using(id_user) where u.id_user='"+Table_click+"'";          
		        try {			            
		            Class.forName("com.mysql.jdbc.Driver");
		            Connection con= (Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/BROKER","root","");
		            Statement stmt=con.createStatement();
		            ResultSet rs = stmt.executeQuery(sql);
		            while (rs.next())
		            {
		                
		                String username= rs.getString("username");
		                textField.setText(username);
		                Boolean aktywny = rs.getBoolean("active");

		                if(aktywny == true)
		                	textField_1.setText("aktywny");
		                else
		                	textField_1.setText("nieaktywny");

		                
		                Boolean rodzaj= rs.getBoolean("status");
		                		//rs.getString("status");
		                if(rodzaj == true)	
		                	textField_2.setText("admin");
		                else
		                	textField_2.setText("user");
		                
		                
		                String index_name= rs.getString("index_name");
		                textField_3.setText(index_name);
		                
		            }
		        }
		        catch(Exception e) {
		         //  JOptionPane.showMessageDialog(null, e.getMessage());
		        }
	            
	            
			}
		});
		table.addPropertyChangeListener(new PropertyChangeListener() {
			public void propertyChange(PropertyChangeEvent arg0) {
				DefaultTableModel model=(DefaultTableModel) table.getModel();
		        model.setRowCount(0);
		        String sql="select id_user, username from user";          
		        try {			            
		            Class.forName("com.mysql.jdbc.Driver");
		            Connection con= (Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/BROKER","root","");
		            Statement stmt=con.createStatement();
		            ResultSet rs = stmt.executeQuery(sql);
		            while (rs.next())
		            {
		               
		                String id_user= rs.getString("id_user");
		                String username= rs.getString("username");

		                model.addRow(new Object []{
		                		id_user, username
		                });			    
		            }
		        }
		        catch(Exception e) {
		           JOptionPane.showMessageDialog(null, e.getMessage());
		        }
			}
		});
		table.setModel(new DefaultTableModel(
			new Object[][] {
			},
			new String[] {
				"ID", "U\u017Cytkownik"
			}
		));
		table.getColumnModel().getColumn(0).setPreferredWidth(40);
		table.getColumnModel().getColumn(0).setMaxWidth(50);
		table.getColumnModel().getColumn(1).setMaxWidth(100);
		scrollPane.setViewportView(table);
		
		JLabel lblNewLabel_2 = new JLabel("U\u017Cytkownik:");
		lblNewLabel_2.setFont(new Font("Tahoma", Font.PLAIN, 11));
		lblNewLabel_2.setBounds(144, 51, 67, 14);
		contentPane.add(lblNewLabel_2);
		
		JLabel lblNewLabel_3 = new JLabel("Status:");
		lblNewLabel_3.setFont(new Font("Tahoma", Font.PLAIN, 11));
		lblNewLabel_3.setBounds(144, 79, 46, 14);
		contentPane.add(lblNewLabel_3);
		
		JLabel lblZarzdzajUytkownikami = new JLabel("Zarzdzaj u\u017Cytkownikami");
		lblZarzdzajUytkownikami.setForeground(SystemColor.textHighlight);
		lblZarzdzajUytkownikami.setFont(new Font("Tahoma", Font.BOLD, 15));
		lblZarzdzajUytkownikami.setBounds(10, 14, 190, 19);
		contentPane.add(lblZarzdzajUytkownikami);
		
		JLabel label = new JLabel("mSerwer");
		label.setForeground(new Color(0, 128, 128));
		label.setFont(new Font("Tahoma", Font.BOLD, 26));
		label.setBounds(302, 8, 122, 23);
		contentPane.add(label);
		
		JLabel lblNewLabel_4 = new JLabel("");
		lblNewLabel_4.setIcon(new ImageIcon("C:\\Users\\marmicc\\Desktop\\PRO\\Broker\\ico\\server.png"));
		lblNewLabel_4.setBounds(10, 219, 53, 48);
		contentPane.add(lblNewLabel_4);
		
		textField = new JTextField();
		textField.setBounds(215, 48, 86, 20);
		contentPane.add(textField);
		textField.setColumns(10);
		
		textField_1 = new JTextField();
		textField_1.setBounds(215, 76, 86, 20);
		contentPane.add(textField_1);
		textField_1.setColumns(10);
		
		JLabel lblNewLabel_5 = new JLabel("Uprawnienia:");
		lblNewLabel_5.setFont(new Font("Tahoma", Font.PLAIN, 11));
		lblNewLabel_5.setBounds(144, 144, 67, 14);
		contentPane.add(lblNewLabel_5);
		
		JTextArea txtrZmianaUprawniania = new JTextArea();
		txtrZmianaUprawniania.setText("Zmiana\r\nuprawniania:");
		txtrZmianaUprawniania.setRows(2);
		txtrZmianaUprawniania.setFont(new Font("Tahoma", Font.PLAIN, 11));
		txtrZmianaUprawniania.setBackground(SystemColor.menu);
		txtrZmianaUprawniania.setBounds(144, 176, 67, 39);
		contentPane.add(txtrZmianaUprawniania);
		
		final JButton btnZastosuj = new JButton("Zastosuj");
		btnZastosuj.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				int row = table.getSelectedRow();
	            String Table_click_idUser = (table.getModel().getValueAt(row, 0).toString());
	            int row2 = table_1.getSelectedRow();
	            String Table_click_idIndex = (table_1.getModel().getValueAt(row2, 0).toString());
	            String Table_click_Index = (table_1.getModel().getValueAt(row2, 1).toString());
	            System.out.println(Table_click_Index);
	            
	        	try{
		             Class.forName("com.mysql.jdbc.Driver");
		             Connection con= (Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/BROKER","root","");
		             Statement stmt=con.createStatement();
		             	String UpdateData ="UPDATE uprawnienia SET id_index='"+Table_click_idIndex+"' WHERE id_user='"+Table_click_idUser+"'";
	            
		            System.out.println(UpdateData);
		            stmt.executeUpdate(UpdateData);	
		            textField_3.setText(Table_click_Index);
		        }
		        catch(Exception eee){
		            eee.printStackTrace();
		        }
	            
			}
		});
		btnZastosuj.setFont(new Font("Tahoma", Font.PLAIN, 11));
		btnZastosuj.setBounds(335, 204, 89, 23);
		contentPane.add(btnZastosuj);
		
		JButton btnNewButton_2 = new JButton("Akt/Deakt");
		btnNewButton_2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				int row = table.getSelectedRow();
	            String Table_click = (table.getModel().getValueAt(row, 0).toString());
				String status = textField_1.getText();
				if(status.equals("aktywny"))
				{
					System.out.println("aktywny");
					
					try{
			             Class.forName("com.mysql.jdbc.Driver");
			             Connection con= (Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/BROKER","root","");
			             Statement stmt=con.createStatement();
			             	String UpdateData ="UPDATE user SET active='0' WHERE id_user='"+Table_click+"'";
		            
			            System.out.println(UpdateData);
			            stmt.executeUpdate(UpdateData);	
			            textField_1.setText("nieaktywny");
			        }
			        catch(Exception eee){
			            eee.printStackTrace();
			        }
				}
				else
				{
					System.out.println("nieaktywny");
					try{
			             Class.forName("com.mysql.jdbc.Driver");
			             Connection con= (Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/BROKER","root","");
			             Statement stmt=con.createStatement();
			             	String UpdateData ="UPDATE user SET active='1' WHERE id_user='"+Table_click+"'";
		            
			            System.out.println(UpdateData);
			            stmt.executeUpdate(UpdateData);	
			            textField_1.setText("aktywny");
			        }
			        catch(Exception eee){
			            eee.printStackTrace();
			        }
				}				
			}
		});
		btnNewButton_2.setFont(new Font("Tahoma", Font.PLAIN, 11));
		btnNewButton_2.setBounds(323, 75, 89, 23);
		contentPane.add(btnNewButton_2);
		
		JButton btnNewButton_3 = new JButton("Odswie\u017C");
		btnNewButton_3.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				DefaultTableModel model=(DefaultTableModel) table.getModel();
		        model.setRowCount(0);
		        String sql="select id_user, username from user";          
		        try {			            
		            Class.forName("com.mysql.jdbc.Driver");
		            Connection con= (Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/BROKER","root","");
		            Statement stmt=con.createStatement();
		            ResultSet rs = stmt.executeQuery(sql);
		            while (rs.next())
		            {
		               
		                String id_user= rs.getString("id_user");
		                String username= rs.getString("username");

		                model.addRow(new Object []{
		                		id_user, username
		                });			    
		            }
		        }
		        catch(Exception e) {
		           JOptionPane.showMessageDialog(null, e.getMessage());
		        }
			}
		});
		btnNewButton_3.setFont(new Font("Tahoma", Font.PLAIN, 11));
		btnNewButton_3.setBounds(10, 192, 124, 23);
		contentPane.add(btnNewButton_3);
		
		textField_2 = new JTextField();
		textField_2.setBounds(215, 107, 86, 20);
		contentPane.add(textField_2);
		textField_2.setColumns(10);
		
		JLabel lblNewLabel_6 = new JLabel("Rodzaj:");
		lblNewLabel_6.setFont(new Font("Tahoma", Font.PLAIN, 11));
		lblNewLabel_6.setBounds(144, 108, 46, 14);
		contentPane.add(lblNewLabel_6);
		
		JScrollPane scrollPane_1 = new JScrollPane();
		scrollPane_1.setBounds(215, 177, 110, 72);
		contentPane.add(scrollPane_1);
		
		table_1 = new JTable();
		table_1.setModel(new DefaultTableModel(
			new Object[][] {
			},
			new String[] {
				"ID", "Index"
			}
		));
		table_1.getColumnModel().getColumn(0).setPreferredWidth(50);
		table_1.getColumnModel().getColumn(0).setMinWidth(30);
		table_1.getColumnModel().getColumn(0).setMaxWidth(100);
		table_1.getColumnModel().getColumn(1).setPreferredWidth(80);
		table_1.getColumnModel().getColumn(1).setMinWidth(80);
		table_1.getColumnModel().getColumn(1).setMaxWidth(150);
		table_1.addPropertyChangeListener(new PropertyChangeListener() {
			public void propertyChange(PropertyChangeEvent arg0) {
				DefaultTableModel model=(DefaultTableModel) table_1.getModel();
		        model.setRowCount(0);
		        String sql="select id_index,index_name from indexy";          
		        System.out.println(sql);
		        try {			            
		            Class.forName("com.mysql.jdbc.Driver");
		            Connection con= (Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/BROKER","root","");
		            /*As we are creating a connection on a local computer we will write the url as jdbc:mysql://localhost:3306 */
		            Statement stmt= con.createStatement();
		            ResultSet rs = stmt.executeQuery(sql);
		            while (rs.next())
		            {
		            	String id_index= rs.getString("id_index");
		                String nazwa= rs.getString("index_name");
		                System.out.println(nazwa);
		               // String kurs= rs.getString("Kurs");
		               // String obroty= rs.getString("Obroty");

		                model.addRow(new Object []{
		                		id_index,nazwa
		                });			    
		            }
		        }
		        catch(Exception e) {
		            JOptionPane.showMessageDialog(null, e.getMessage());
		        }
			}
		});
		scrollPane_1.setViewportView(table_1);
		
		textField_3 = new JTextField();
		textField_3.setBounds(215, 141, 86, 20);
		contentPane.add(textField_3);
		textField_3.setColumns(10);
		
		JButton btnNewButton_1 = new JButton("User/admin");
		btnNewButton_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				int row = table.getSelectedRow();
	            String Table_click = (table.getModel().getValueAt(row, 0).toString());
				String status = textField_2.getText();
				if(status.equals("admin"))
				{
					System.out.println("admin");
					
					try{
			             Class.forName("com.mysql.jdbc.Driver");
			             Connection con= (Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/BROKER","root","");
			             Statement stmt=con.createStatement();
			             	String UpdateData ="UPDATE user SET status='0' WHERE id_user='"+Table_click+"'";
		            
			            System.out.println(UpdateData);
			            stmt.executeUpdate(UpdateData);	
			            textField_2.setText("user");
			        }
			        catch(Exception eee){
			            eee.printStackTrace();
			        }
				}
				else
				{
					System.out.println("user");
					try{
			             Class.forName("com.mysql.jdbc.Driver");
			             Connection con= (Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/BROKER","root","");
			             Statement stmt=con.createStatement();
			             	String UpdateData ="UPDATE user SET status='1' WHERE id_user='"+Table_click+"'";
		            
			            System.out.println(UpdateData);
			            stmt.executeUpdate(UpdateData);	
			            textField_2.setText("admin");
			        }
			        catch(Exception eee){
			            eee.printStackTrace();
			        }
				}
			}
		});
		btnNewButton_1.setFont(new Font("Tahoma", Font.PLAIN, 11));
		btnNewButton_1.setBounds(323, 109, 89, 23);
		contentPane.add(btnNewButton_1);
	}
}
